//
//  BaseViewController.swift
//  SwiftLoopers
//
//  Created by MAC on 2017/8/21.
//  Copyright © 2017年 Kevin. All rights reserved.
//

import UIKit

class BaseViewController: UIViewController {
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return StatusBarStyle
        
    }
    var BarStyle:UIStatusBarStyle!
    private var isShowleft:Bool!
    var isShowLiftBack:Bool{
        get{
            return isShowleft
        }
        set{
            isShowleft = newValue
            if let VCcount = self.navigationController?.viewControllers.count {
                if (VCcount >= 1  || self.navigationController?.presentingViewController != nil) && isShowleft {
                    
                }else{
                    
                }

            }
        }
    }
    var isHidenNaviBar:Bool!


    var StatusBarStyle:UIStatusBarStyle {
        get {
            return BarStyle
        }
        set {
            BarStyle = newValue
            setNeedsStatusBarAppearanceUpdate()
        }
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        UIApplication.shared.keyWindow?.endEditing(true)
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.white
        self.BarStyle = UIStatusBarStyle.lightContent
        self.StatusBarStyle = self.BarStyle
        self.isShowLiftBack = true
        // Do any additional setup after loading the view.
    }
    func addNavigationItemWithTitles(_ titles:NSArray, _ isLeft:Bool, _ target:Any, _ action:Selector, _ tags:NSArray)  {
        var Items:NSMutableArray!
        let spaceItem = UIBarButtonItem.init(barButtonSystemItem: UIBarButtonSystemItem(rawValue:6)!, target: nil, action: nil)
        
        spaceItem.width = -10
        Items.add(spaceItem)
        for i in 0..<titles.count{
            let title = titles[i] as!String
            let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 20))
            btn.setTitle(title, for: UIControlState(rawValue: 0))
            btn.setTitleColor(UIColor.white, for: UIControlState.normal)
            btn.titleLabel?.font = UIFont.systemFont(ofSize: 16)
            btn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
            btn.tag = tags[i] as! NSInteger
            
            let item = UIBarButtonItem(customView: btn)
            
            Items.add(item)

        }
        
        if isLeft {
            self.navigationItem.leftBarButtonItems = Items as? [UIBarButtonItem]
        }else{
            self.navigationItem.rightBarButtonItems = Items as? [UIBarButtonItem]
        }

    }
    
    
    func addNavigationItemWithImageNames(_ imageNames:NSArray, _ isLeft:Bool, _ target:Any, _ action:Selector, _ tags:NSArray) {
        var Items:NSMutableArray!
        
        for i in 0..<imageNames.count{
            let imageName = imageNames[i]
            let btn = UIButton(frame: CGRect(x: 0, y: 0, width: 30, height: 30))
            btn.setImage( UIImage.init(named: imageName as! String), for: UIControlState.normal)
            if isLeft {
                btn.imageEdgeInsets = UIEdgeInsetsMake(0, 10, 0, -10)
            }else{
                btn.imageEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 10)
            }
            
            btn.addTarget(target, action: action, for: UIControlEvents.touchUpInside)
            btn.tag = tags[i] as! NSInteger
            
            let item = UIBarButtonItem(customView: btn)
            
            Items.add(item)
            
        }
        
        if isLeft {
            self.navigationItem.leftBarButtonItems = Items as? [UIBarButtonItem]
        }else{
            self.navigationItem.rightBarButtonItems = Items as? [UIBarButtonItem]
        }
    }
    
    func backBtnClicked(_ animation:Bool)  {
        if self.presentingViewController != nil {
            self.dismiss(animated: animation, completion: nil)
        }else {
            self.navigationController?.popViewController(animated: animation)
        }
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
